// pages/doctorDetail/index.js
var http = require('../../../utils/http');
var api = require('../../../utils/api');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        disabledBtn:false,
        showOverPopup:false,
        i: 1,
        doc_id: "",
        childInfo: "",
        dataList: [],
        childInfo: '',
        showPopup: false,
        hosname:""
    },
    onClosePopup() {
        this.setData({
            showPopup: false,
            showOverPopup: false,
        })
    },
    changeTab(e) {
        this.setData({
            i: e.currentTarget.dataset.index
        })
    },
    getInfo() {
        http.postHttp(api.getDocHos, {
                child_id: this.data.childInfo.id,
                doc_id: this.data.doc_id,
            },
            (result) => {
                if (result.code == 0) {
                    result.data.forEach(ele => {
                        ele.status = false
                        if (ele.hos_sche.length > 3) {
                            ele.status = true
                            ele.hos_sche_small = ele.hos_sche.slice(0, 3)
                        }
                    })
                    this.setData({
                        dataList: result.data
                    })
                    // console.log(this.data.dataList);
                }
            },
            (err) => {
                console.log(err);
            },
        )
    },
    changeAllFun(e) {
        let parentindex = e.currentTarget.dataset.index, //点击的内层所在的最外层列表下标
            list = this.data.dataList;
        list[parentindex].status = !list[parentindex].status || false; //变换其打开、关闭的状态
        this.setData({
            dataList: list
        });
    },
    init() {
        const childInfo = wx.getStorageSync('childInfo')
        const userInfo = wx.getStorageSync('userInfo')
        this.setData({
            childInfo: childInfo,
            userInfo: userInfo
        })
        this.getInfo()
    },
    /** 新建预约 */
    postResrve(item,hosname) {
        http.postHttp(api.postResrve, {
                "data": {
                    "child_id": this.data.childInfo.id,
                    "user_id": this.data.userInfo.id,
                    "reserver_time": item.work_time,
                    "public_schedule_id": item.id
                },
                "type": "3"
            },
            (result) => {
                if (result.code == 0) {
                    this.setData({
                        timer: item.work_time,
                        doc_name: this.data.info.doc_name,
                        hosname:hosname,
                        showPopup: true,
                        disabledBtn:false
                    })
                }
            },
            (err) => {
                console.log(err);
            },
        )
    },
    /** 预约 */
    handleYuyue(e) {
        this.setData({
            disabledBtn:true
        })
        let item = e.currentTarget.dataset.item
        let hosname = e.currentTarget.dataset.hosname
        /** 先检测是否可以预约 */
        http.postHttp(api.isIfBook, {
                rtype: "3",
                personal_id: this.data.childInfo.personal_id
            },
            (result) => {
                if (result.code == 0) {
                    if (result.data.ifok) {
                        this.postResrve(item,hosname)
                    } else {
                        console.log("不能预约");
                        this.setData({
                            showOverPopup: true,
                            disabledBtn:false
                        })
                    }
                }
            },
            (err) => {
                console.log(err);
            },
        )
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({
            doc_id: options.docid,
            info: JSON.parse(options.info)
        })
        this.init()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})